package top.shiyiri.exercise;

import top.shiyiri.util.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Scanner;

/**
 * @author Aunean
 * @date 2022/1/19 16:36
 */
public class ExerciseTest1 {

    //通用的增删改操作
    public static int update(String sql, Object ... args) {
        Connection connection = null;
        PreparedStatement ps = null;
        try {
            //1.获取连接
            connection = JDBCUtils.getConnection();
            //2.预编译SQL语句，返回PreparedStatement的实例
            ps = connection.prepareStatement(sql);
            //3.填充占位符
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i+1, args[i]);
            }
            //执行操作
            //ps.execute();
            int i = ps.executeUpdate();
            return i;
        } catch (Exception e) {

            e.printStackTrace();
        } finally {
            //关闭资源
            JDBCUtils.closeResource(connection, ps);
        }
        return 0;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入用户名：");
        String name = scanner.next();
        System.out.println("输入用户邮箱：");
        String email = scanner.next();
        System.out.println("输入用户生日：");
        String birth = scanner.next();

        String sql = "insert into customers(name, email, birth) values(?,?,?)";
        int update = update(sql, name, email, birth);
        if (update > 0) {
            System.out.println("操作成功");
        }
    }

}
